import json

from django.shortcuts import render
from db_man_app.models import Class, Major, Department, Student
from django.db.models import Count
from django.http import JsonResponse
# Create your views here.
def get_all(request):
    # 获取所有班级及其关联数据
    classes = Class.objects.select_related(
        'major',
        'major__department'
    ).all()
    # 构建返回数据
    tableData = []
    for cls in classes:
        tableData.append({
            'class_name': cls.class_name,
            'major_name': cls.major.major_name,
            'college_name': cls.major.department.department_name,
            'grade': cls.grade,
            'class_p_number': cls.student_count
        })
    return JsonResponse({
        'tableData': tableData,
        'status': 'success',
        'message': '班级数据获取成功'
    })

def get_major_form(request):
    majors = Major.objects.select_related('department').all()
    data = [{
        'major_id': major.major_id,
        'major_name': major.major_name,
        'department_id': major.department.department_id,
        'department_name': major.department.department_name
    } for major in majors]

    return JsonResponse({
        'code': 200,
        'message': 'success',
        'data': data
    })

def add_one(request):
    data=json.loads(request.body)
    class_name = data['class_name']
    department_id = data['department_id']
    major_id = data['major_id']
    grade = data['grade']

    print(class_name, department_id, major_id, grade)
    try:
        Class.objects.create(
            class_name=class_name,
            department_id=department_id,
            major_id=major_id,
            grade=grade,
            student_count=0
        )
        return JsonResponse(1,safe=False)
    except Exception as e:
        return JsonResponse(0,safe=False)


def updata_one(request):
    data=json.loads(request.body)
    old_name = data['old_name']
    class_name = data['formData']['class_name']
    major_name = data['formData']['major_name']
    grade = data['formData']['grade']
    #获取专业对应的id
    major_id=Major.objects.filter(major_name=major_name).first().major_id
    try:
        Class.objects.filter(class_name=old_name).update(
            class_name=class_name,
            major_id=major_id,
            grade=grade,
        )
        #获得对应的学院id，将所属的所有学生的学院id改了。
        # 再把学生的专业都改了
        college_id=Major.objects.filter(major_id=major_id).first().department_id
        class_id=Class.objects.filter(class_name=class_name).first().class_id
        Student.objects.filter(class_field=class_id).update(
            department_id=college_id,
            major_id=major_id,
        )

        return JsonResponse(1,safe=False)
    except Exception as e:
        print(e)
        return JsonResponse(0,safe=False)

def del_one(request):
    data=json.loads(request.body)
    del_name = data['del_id']
    try:
        Class.objects.filter(class_name=del_name).delete()
        return JsonResponse(1,safe=False)
    except Exception as e:
        print(e)
        return JsonResponse(0,safe=False)
